package com.finance.system.configure.websecurity;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finance.system.user.entity.UserEntity;
import com.finance.system.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Component("mobileUserService")
public class MobileUserService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",s);
        UserEntity user = userMapper.selectOne(wrapper);
        if(user == null){
            return null;
        }
        //获取最新的手机号验证码(本地db或远程接口调用)
        String code = "876422";
        return User.withUsername(s).password(passwordEncoder.encode(code))
                .authorities(Collections.emptyList()).build();
    }
}
